package school.community.service;

import school.community.mapper.UserMapper;
import school.community.model.User;
import school.community.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
  @Autowired
  private UserMapper userMapper;

  public void createOrUpdate(User user) {
    UserExample userExample = new UserExample();
    userExample.createCriteria()
            .andAccountIdEqualTo(user.getAccountId());
    List<User> users = userMapper.selectByExample(userExample);
    if (users.size() == 0) {
      // 插入
      user.setGmtCreate(System.currentTimeMillis());
      user.setGmtModified(user.getGmtCreate());
      userMapper.insert(user);
    } else {
      //更新
      User dbUser = users.get(0);
      User updateUser = new User();
      updateUser.setGmtModified(System.currentTimeMillis());
      updateUser.setAvatarUrl(user.getAvatarUrl());
      updateUser.setName(user.getName());
      updateUser.setToken(user.getToken());
      UserExample example = new UserExample();
      example.createCriteria()
              .andIdEqualTo(dbUser.getId());
      userMapper.updateByExampleSelective(updateUser, example);
    }
  }

  public User getUserByAccountId(User user) {
    UserExample userExample = new UserExample();
    userExample.createCriteria()
            .andAccountIdEqualTo(user.getAccountId());
    List<User> users = userMapper.selectByExample(userExample);
    User dbUser = new User();
    if (users.size() != 0) {
      dbUser = users.get(0);
    }
    return dbUser;
  }
}
